package entity;

/**
 * @Author: wanqian
 * @Date: 2021/2/4 15:05
 */
public class TreeNode {

    private int id;

    private String name;

    private TreeNode left;

    private TreeNode right;

    private int leftType;

    private int rightType;

    public TreeNode(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public void deleteById(int id) {
        if (left != null && this.left.getId() == id) {
            this.setLeft(null);
            return;
        }

        if (right != null && this.right.getId() == id) {
            this.setRight(null);
            return;
        }

        if (left != null) {
            this.left.deleteById(id);
        }
        if (right != null) {
            this.right.deleteById(id);
        }
    }

    public void preOrder() {
        System.out.println(this);
        if (left != null && leftType == 0) {
            this.left.preOrder();
        }

        if (right != null && rightType == 0) {
            this.right.preOrder();
        }
    }

    public TreeNode preOrderSearch(int id) {
        System.out.println("查找节点：" + this);
        if (this.id == id) {
            return this;
        }
        TreeNode result = null;
        if (this.left != null) {
            result = this.left.preOrderSearch(id);
            if (result != null) {
                return result;
            }
        }

        if (this.right != null) {
            result = this.right.preOrderSearch(id);
        }

        return result;
    }

    public void infixOrder() {
        if (left != null && leftType == 0) {
            this.left.infixOrder();
        }
        System.out.println(this);

        if (right != null && rightType == 0) {
            this.right.infixOrder();
        }
    }

    public TreeNode infixOrderSearch(int id) {
        TreeNode result = null;
        if (this.left != null) {
            result = this.left.infixOrderSearch(id);
            if (result != null) {
                return result;
            }
        }
        System.out.println("查找节点：" + this);
        if (this.id == id) {
            return this;
        }

        if (this.right != null) {
            result = this.right.infixOrderSearch(id);
        }

        return result;
    }

    public void postOrder() {
        if (left != null) {
            this.left.postOrder();
        }

        if (right != null) {
            this.right.postOrder();
        }

        System.out.println(this);
    }

    public TreeNode postOrderSearch(int id) {
        TreeNode result = null;
        if (this.left != null) {
            result = this.left.postOrderSearch(id);
            if (result != null) {
                return result;
            }
        }

        if (this.right != null) {
            result = this.right.postOrderSearch(id);
            if (result != null) {
                return result;
            }
        }

        System.out.println("查找节点：" + this);
        if (this.id == id) {
            return this;
        }

        return result;
    }

    @Override
    public String toString() {
        return "TreeNode{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", left=" + (left == null ? "" : left.name) +
                ", right=" + (right == null ? "" : right.name) +
                ", leftType=" + leftType +
                ", rightType=" + rightType +
                '}';
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public TreeNode getLeft() {
        return left;
    }

    public void setLeft(TreeNode left) {
        this.left = left;
    }

    public TreeNode getRight() {
        return right;
    }

    public void setRight(TreeNode right) {
        this.right = right;
    }

    public int getLeftType() {
        return leftType;
    }

    public void setLeftType(int leftType) {
        this.leftType = leftType;
    }

    public int getRightType() {
        return rightType;
    }

    public void setRightType(int rightType) {
        this.rightType = rightType;
    }
}
